#! /bin/sh

#---------------------------------------------------------------------
#                          make-post-remove                           
# 
# rewrite package hooks to integrate with erlrc.
#---------------------------------------------------------------------

#---------------------------------------------------------------------
#                           chicken and egg                           
#---------------------------------------------------------------------

test -f "${FW_ROOT}/share/fw/sh/fw-find.sh" && fws="${FW_ROOT}/share/fw/sh/fw-find.sh"
test -f "fw/sh/fw-find.sh" && fws="fw/sh/fw-find.sh"
test -f "fw.local/sh/fw-find.sh" && fws="fw.local/sh/fw-find.sh"
test ! -z "$fws" || {
  echo "erlang-rebar/make-post-remove: fatal: can't locate fw-find.sh" 1>&2
  exit 1
}

. "$fws"

#---------------------------------------------------------------------
#                              routines                               
#---------------------------------------------------------------------

fw_source "erlang-rebar/make-post-remove" "sh/fw-exec.sh"
fw_source "erlang-rebar/make-post-remove" "sh/parse-args.sh"
fw_source "erlang-rebar/make-post-remove" "sh/validate-args.sh"

#---------------------------------------------------------------------
#                                main                                 
#---------------------------------------------------------------------

parse_args "erlang-rebar/make-post-remove" "$@"

validate_args "erlang-rebar/make-post-remove" "destdir erlrc libexecdir package" "package"

test "x$erlrc" = x0 || {
  eval `fw_exec "template/erlang-rebar/load-config"`
  
  hookdir="${libexecdir}/fw/$FW_PACKAGE_NAME-$FW_PACKAGE_VERSION"
  
  test -d "${destdir}/${hookdir}" || fatal "erlang-rebar/make-post-remove"
  
  test -x "${destdir}/${hookdir}/post-remove" && 
  mv "${destdir}/${hookdir}/post-remove" \
     "${destdir}/${hookdir}/post-remove.orig"
  
  fw_exec "package/$package/make-hook" --hook post-remove       \
    --upgrade "
    if \$compare_versions \
            \"\$canon_old_version\" lt \"$FW_PACKAGE_VERSION\" ; then
      # package upgrade is to a later version; invoke erlrc-upgrade
      which erlrc-upgrade >/dev/null 2>/dev/null
      test \$? -ne 0 || \
      erlrc-upgrade \"$FW_ERL_APP_NAME\" \"\$canon_old_version\" \"$FW_PACKAGE_VERSION\" || \
      exit 1
    else
      # package upgrade is to an earlier version; invoke erlrc-downgrade
      which erlrc-downgrade >/dev/null 2>/dev/null
      test \$? -ne 0 || \
      erlrc-downgrade \"$FW_ERL_APP_NAME\" \"$FW_PACKAGE_VERSION\" \"\$canon_old_version\" || \
      exit 1
    fi
"                                                               \
    --postamble "
TMPDIR=\${TMPDIR:-\"/tmp\"}

if test -x \"\${TMPDIR}/.$FW_PACKAGE_NAME.$FW_PACKAGE_VERSION.post-remove.orig\"
  then
    set -e
    \"\${TMPDIR}/.$FW_PACKAGE_NAME.$FW_PACKAGE_VERSION.post-remove.orig\" \"\$@\" 
    set +e
  fi
" > "${destdir}/${hookdir}/post-remove" &&                      \
  chmod +x "${destdir}/${hookdir}/post-remove"
}
